import math
from collections import OrderedDict


tmp_subject_score_list = [
    ['a',1],
    ['b',2],
    ['c',4],
    ['d',3],
    ['e',float('nan')],
    ['f',float('nan')],
]
subject_pageids_dict = {
    'a':[1,3,4],
    'b':[3,4,6],
    'c':[8,7],
    'd':[9,10],
    'e':[11,1,12],
    'f':[12,13]
}

id_list = [[x,1] for x in range(13,0,-1)]
id_list = [[x,1] for x in range(1,14,-1)]

# 将 list 转化为 dict
id_list_rank_dict = {}
for i,(k,v) in enumerate(id_list):
	id_list_rank_dict[k] = i

# 合并相同分数的 subject
subject_rank_dict = OrderedDict()
for subject_name,score in tmp_subject_score_list:
	if math.isnan(score):
		subject_rank_dict.setdefault('nan',set())
		subject_rank_dict['nan'].add(subject_name)
	else:
		subject_rank_dict.setdefault(score,set())
		subject_rank_dict[score].add(subject_name)

# 将 subject 转换到 pageid
page_id_rank_summary = OrderedDict()
for score, subject_name_set in subject_rank_dict.items():
	#同一分数下的所有 page_id
	page_ids_set = set()
	for subject_name in subject_name_set:
		page_ids_set.update(subject_pageids_dict[subject_name])
	page_ids_list = list(page_ids_set)
	page_ids_list.sort(key=lambda x: id_list_rank_dict.get(x,999))
	for page_id in page_ids_list:
		page_id_rank_summary.setdefault(page_id,None)
print(page_id_rank_summary)